package org.jeecg.modules.solitaire_app.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.VO.ProvinceJyzNumVO;
import org.jeecg.modules.solitaire_app.app.entity.YsyDrivers;
import org.jeecg.modules.solitaire_app.app.entity.YsyJyz;
import org.jeecg.modules.solitaire_app.app.entity.YsyProvinceCar;
import org.jeecg.modules.solitaire_app.app.mapper.YsyDriversMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyDriversService;
import org.jeecg.modules.solitaire_app.app.service.IYsyProvinceCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 司机信息
 * @Author: jeecg-boot
 * @Date: 2023-11-25
 * @Version: V1.0
 */
@Service
@DS("ysy_db")
@Slf4j
public class YsyDriversServiceImpl extends ServiceImpl<YsyDriversMapper, YsyDrivers> implements IYsyDriversService {

    @Autowired
    private IYsyProvinceCarService ysyProvinceCarService;

    @Override
    public Result<PageInfo<YsyDrivers>> getYsyDrivers(Map<String, Object> map) {
        Result<PageInfo<YsyDrivers>> result = new Result<>();
        try {

            String province = map.get("province").toString();

            YsyProvinceCar ysyProvinceCar = ysyProvinceCarService.getOne(Wrappers.<YsyProvinceCar>lambdaQuery().like(YsyProvinceCar::getProvince, province));

            map.put("provinceName", ysyProvinceCar.getCarNo());

            PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString()));

            List<YsyDrivers> drivers = this.baseMapper.getYsyDrivers(map);

            PageInfo<YsyDrivers> pageInfo = new PageInfo<>(drivers);
            result.setCode(200);
            result.setResult(pageInfo);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<PageInfo<YsyJyz>> getYsyJyz(Map<String, Object> map) {
        Result<PageInfo<YsyJyz>> result = new Result<>();
        try {
            PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString()));

            List<YsyJyz> drivers = this.baseMapper.getYsyJyz(map);

            PageInfo<YsyJyz> pageInfo = new PageInfo<>(drivers);
            result.setCode(200);
            result.setResult(pageInfo);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<List<ProvinceJyzNumVO>> getProvinceCarNumVO() {
        Result<List<ProvinceJyzNumVO>> result = new Result<>();
        Map<String, Object> map = new HashMap<>();
        try {
            List<YsyDrivers> ysyDrivers = this.baseMapper.getYsyDrivers(map);
            Map<String, Integer> provinceMap = new HashMap<>();
            int noCarNum = 0;
            provinceMap.put("无", noCarNum);
            ysyDrivers.stream().forEach(item -> {
                if (ObjectUtil.isNotEmpty(item.getCarNo())) {
                    String provinceName = String.valueOf(item.getCarNo().charAt(0));
                    if (provinceMap.containsKey(provinceName)) {
                        int currentValue = provinceMap.get(provinceName);
                        provinceMap.put(provinceName, currentValue + 1);
                    } else {
                        provinceMap.put(provinceName, 1);
                    }
                } else {
                    int noValue = provinceMap.get("无");
                    provinceMap.put("无", noValue + 1);
                }
            });
            List<ProvinceJyzNumVO> provinceJyzNumVOS = new ArrayList<>();
            for (Map.Entry<String, Integer> entry : provinceMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                if (key != null) {
                    ProvinceJyzNumVO provinceJyzNumVO = new ProvinceJyzNumVO();
                    provinceJyzNumVO.setName(key);
                    provinceJyzNumVO.setValue(value);
                    provinceJyzNumVOS.add(provinceJyzNumVO);
                }
            }
            Collections.sort(provinceJyzNumVOS);
            result.setCode(200);
            result.setSuccess(true);
            result.setResult(provinceJyzNumVOS);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<List<ProvinceJyzNumVO>> getProvinceJyzNumVO() {
        Result<List<ProvinceJyzNumVO>> result = new Result<>();
        try {
            List<ProvinceJyzNumVO> ysyDrivers = this.baseMapper.getProvinceJyzNumVO();
            result.setCode(200);
            result.setSuccess(true);
            result.setResult(ysyDrivers);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<Integer> getJyzNum() {
        Result<Integer> result = new Result<>();
        try {
            int count = this.baseMapper.getJyzNum();
            result.setCode(200);
            result.setSuccess(true);
            result.setResult(count);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<Integer> getCarNum() {
        Result<Integer> result = new Result<>();
        try {
            int count = this.baseMapper.getCarNum();
            result.setCode(200);
            result.setSuccess(true);
            result.setResult(count);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage("服务器响应失败");
            result.setSuccess(false);
            return result;
        }
    }
}
